package com.insurance.utils;

import com.insurance.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author qcjstudy
 * @create 2022-12-01 11:20
 */
public class SecurityUtils {
    /**
     * 获取登录用户
     * @return LoginUser
     */
    public static LoginUser getLoginUser(){
        System.out.println(getAuthentication().getPrincipal());
        return (LoginUser) getAuthentication().getPrincipal();
    }

    /**
     * 获取Authentication
     * @return Authentication
     */
    private static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 判断是否为管理员
     * @return Boolean
     */
    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id!=null&&1L==id;
    }

    /**
     * 获取用户id
     * @return
     */
    public static Long getUserId(){
        return getLoginUser().getUser().getId();
    }
}
